<?php

App::uses('AppController', 'Controller');

class SitemapController extends AppController {
    
    public $components = array('RequestHandler');
    public $uses       = array('Group');
    public $layout     = 'ajax';
    
    public function index() {
        $urls = array(
            $this->addUrl('/'),
            $this->addUrl('/ukusi'),
            $this->addUrl('/cene'),
            $this->addUrl('/kontakt')
        );
        
        $groups = $this->Group->find('list');
        foreach($groups as $id=>$name) {
            $urls[] = $this->addUrl('/proizvodi/'.urlize($name).'/'.$id);
        }
        
        $this->set('urlset', $urls);
        $this->RequestHandler->respondAs('xml');
    }
    
    private function addUrl($url, $lastmod=null, $freq=null, $priority=null) {
        if (! $lastmod)  $lastmod  = date('Y-m-d', time() - 60*60*24*10);
        if (! $freq)     $freq     = 'weekly';
        if (! $priority) $priority = 0.8;
        
        $url = Router::url($url, true);
        return array(
            'loc'      => $url,
            'lastmod'  => $lastmod,
            'freq'     => $freq,
            'priority' => $priority,
        );
    }
}